package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.commission.*;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.commission.*;
import com.miplus.ccrm.core.service.utils.LogXxlJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 2-12.佣金服务接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/commission")
public class CommissionController {
    @Resource
    private FiCommissionTemplateService fiCommissionTemplateService;
    @Resource
    private FiCommissionAgentModeService fiCommissionAgentModeService;
    @Resource
    private FiCommissionAgentCountService fiCommissionAgentCountService;
    @Resource
    private FiCommissionService fiCommissionService;
    @Resource
    private FiCommissionKpiService fiCommissionKpiService;
    @Resource
    private FiCommissionKpiRatioService fiCommissionKpiRatioService;

    @Resource
    FiCasesRepaymentDetailsService repaymentDetailsService;

    @Resource
    private FiCommissionAgentService fiCommissionAgentService;
    @Resource
    private FiBillReportService fiBillReportService;

    @Resource
    private FiCasesRepaymentStatisticsService fiCasesRepaymentStatisticsService;

    @Resource
    private FiDeptRepaymentReportService fiDeptRepaymentReportService;

    @Resource
    private FiAgentRepayReportService fiAgentRepayReportService;

    @Resource
    private FiCommissionAmtService fiCommissionAmtService;


    @Resource
    private LogXxlJobService logXxlJobService;

    @Resource
    private JCommonData jCommonData;

    /**
     * 取委案队列佣金计算模式列表
     */
    @GetMapping("/templateList")
    public R<PageData<CommissionTempResVo>> templateList(@Validated CommissionTempListReqVo commissionTempListReqVo){
        return R.SuccessPage(fiCommissionTemplateService.getCommissionTemplateList(commissionTempListReqVo));
    }

    /**
     * 添加佣金计算方式
     */
    @PostMapping("/addTempalte")
    public R<CmdResVo> addTemplate(@RequestBody @Validated  FiCommissionTemplate fiCommissionTemplate) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionTemplateService.addCommissionTemplate(usrid,fiCommissionTemplate));
    }

    /**
     * 修改佣金计算方式。
     */
    @PostMapping("/updateTemplate")
    public R<CmdResVo> updateTemplate(@RequestBody @Validated CommissionTempEditVo commissionTempEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionTemplateService.updateCommissionTemplate(usrid,commissionTempEditVo));
    }

    /**
     * 删除佣金计算方式。
     */
    @PostMapping("/deleteTemplate")
    public R<CmdResVo> deleteTemplate(@RequestBody @Validated CommissionTempEditVo commissionTempEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionTemplateService.deleteCommissionTemplate(commissionTempEditVo.getId()));
    }
    /**
     * 获取佣金计算方式。
     */
    @GetMapping("/getAvailTemplate")
    public R<FiCommissionTemplate> getAvailTemplate(@Validated CommissionTempReqVo para) {
//        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionTemplateService.getAvailTemplate(para.getDisttag(), para.getDay()));
    }

    /**
     * 取委案队催员数量计算方式
     */
    @GetMapping("/agentModeList")
    public R<PageData<AgentModeResVo>> agentModeList(@Validated AgentModeReqVo agentModeReqVo){
        return R.SuccessPage(fiCommissionAgentModeService.getAgentModeList(agentModeReqVo));
    }

    /**
     * 添加催员计算方式
     */
    @PostMapping("/addAgentMode")
    public R<CmdResVo> addAgentMode(@RequestBody @Validated  FiCommissionAgentMode fiCommissionAgentMode) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentModeService.addAgentMode(usrid,fiCommissionAgentMode));
    }

    /**
     * 修改催员计算方式。
     */
    @PostMapping("/updateAgentMode")
    public R<CmdResVo> updateAgentMode(@RequestBody @Validated AgentModeEditVo agentModeEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentModeService.updateAgentMode(usrid,agentModeEditVo));
    }

    /**
     * 删除催员计算方式。
     */
    @PostMapping("/deleteAgentMode")
    public R<CmdResVo> deleteAgentMode(@RequestBody @Validated AgentModeEditVo agentModeEditVo) {
        //String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentModeService.deleteAgentMode(agentModeEditVo.getModeid()));
    }

    /**
     * 查询委案机构坐席数量。
     */
    @GetMapping("/selectAgentCount")
    public R<PageData<AgentCountResVo>> selectAgentCount(@Validated AgentCountReqVo agentCountReqVo){
        return R.SuccessPage(fiCommissionAgentCountService.getAgentCountList(agentCountReqVo));
    }

    /**
     * 添加委外机构催员数量
     */
    @PostMapping("/addAgentCount")
    public R<CmdResVo> addAgentCount(@RequestBody @Validated  FiCommissionAgentCount fiCommissionAgentCount) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentCountService.addAgentCount(usrid,fiCommissionAgentCount));
    }

    /**
     * 修改催员数量。
     */
    @PostMapping("/updateAgentCount")
    public R<CmdResVo> updateAgentCount(@RequestBody @Validated AgentCountEditVo agentCountEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentCountService.updateAgentCount(usrid,agentCountEditVo));
    }

    /**
     * 删除催员数量。
     */
    @PostMapping("/deleteAgentCount")
    public R<CmdResVo> deleteAgentCount(@RequestBody @Validated AgentCountEditVo agentCountEditVo) {
        //String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentCountService.deleteAgentCount(agentCountEditVo.getId()));
    }
    /**
     * 查询委案机构佣金比例
     */
    @GetMapping("/selectCommissionRatio")
    public R<PageData<CommissionRatioResVo>> selectCommissionRatio(@Validated CommissionRatioListReqVo para){
        return R.SuccessPage(fiCommissionService.getCommissionRatioList(para));
    }

    /**
     * 添加委外机构佣金比例
     */
    @PostMapping("/addCommissionRatio")
    public R<CmdResVo> addCommissionRatio(@RequestBody @Validated  FiCommission fiCommission) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionService.addCommissionRatio(usrid,fiCommission));
    }

    /**
     * 修改委婉机构佣金比例。
     */
    @PostMapping("/updateCommissionRatio")
    public R<CmdResVo> updateCommissionRatio(@RequestBody @Validated CommissionRatioEditVo para) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionService.updateCommissionRatio(usrid,para));
    }

    /**
     * 删除佣金比例。
     */
    @PostMapping("/deleteCommissionRatio")
    public R<CmdResVo> deleteCommissionRatio(@RequestBody @Validated CommissionRatioEditVo para) {
        //String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionService.deleteCommissionRatio(para.getId()));
    }

    /**
     * 查询委催员单价。
     */
    @GetMapping("/selectAgentCommissionList")
    public R<PageData<AgentCommissionResVo>> selectAgentCommissionList(@Validated AgentCommissionListReqVo para){
        return R.SuccessPage(fiCommissionAgentService.getAgentCommissionList(para));
    }

    /**
     * 添加催员单价
     */
    @PostMapping("/addAgentCommission")
    public R<CmdResVo> addAgentCommission(@RequestBody @Validated  FiCommissionAgent para) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentService.addAgentCommission(usrid,para));
    }

    /**
     * 修改催员单价。
     */
    @PostMapping("/updateAgentCommission")
    public R<CmdResVo> updateAgentCommission(@RequestBody @Validated AgentCommissionEditVo para) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentService.updateAgentCommission(usrid,para));
    }

    /**
     * 删除催员单价。
     */
    @PostMapping("/deleteAgentCommission")
    public R<CmdResVo> deleteAgentCommission(@RequestBody @Validated AgentCommissionEditVo para) {
        //String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAgentService.deleteAgentCommission(para.getId()));
    }
    /**
     * 查询KPI目标列表
     */
    @GetMapping("/selectCommissionKpi")
    public R<PageData<CommissionKpiResVo>> selectCommissionKpi(@Validated CommissionKpiListReqVo para){
        return R.SuccessPage(fiCommissionKpiService.getCommissionKpiList(para));
    }
    /**
     * 添加KPI目标
     */
    @PostMapping("/addCommissionKpi")
    public R<CmdResVo> addCommissionKpi(@RequestBody @Validated  FiCommissionKpi fiCommissionKpi) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionKpiService.addCommissionKpi(usrid,fiCommissionKpi));
    }

    /**
     * 修改KPI目标。
     */
    @PostMapping("/updateCommissionKpi")
    public R<CmdResVo> updateCommissionKpi(@RequestBody @Validated CommissionKpiEditVo para) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionKpiService.updateCommissionKpi(usrid,para));
    }

    /**
     * 删除KPI目标。
     */
    @PostMapping("/deleteCommissionKpi")
    public R<CmdResVo> deleteCommissionKpi(@RequestBody @Validated CommissionKpiEditVo para) {
        //String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionKpiService.deleteCommissionKpi(para.getId()));
    }


    /**
     * 查询KP考核系数列表
     */
    @GetMapping("/selectCommissionRatioKpi")
    public R<PageData<CommissionKpiRatioResVo>> selectCommissionRatioKpi(@Validated CommissionKpiRatioListReqVo para){
        return R.SuccessPage(fiCommissionKpiRatioService.getCommissionKpiRatioList(para));
    }
    /**
     * 添加KPI考核系数
     */
    @PostMapping("/addCommissionKpiRatio")
    public R<CmdResVo> addCommissionKpiRatio(@RequestBody @Validated  FiCommissionKpiRatio fiCommissionKpiRatio) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionKpiRatioService.addCommissionKpiRatio(usrid,fiCommissionKpiRatio));
    }

    /**
     * 修改KPI考核系数。
     */
    @PostMapping("/updateCommissionKpiRatio")
    public R<CmdResVo> updateCommissionKpiRatio(@RequestBody @Validated CommissionKpiRatioEditVo para) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionKpiRatioService.updateCommissionKpiRatio(usrid,para));
    }

    /**
     * 删除KPI考核系数。
     */
    @PostMapping("/deleteCommissionKpiRatio")
    public R<CmdResVo> deleteCommissionKpiRatio(@RequestBody @Validated CommissionKpiRatioEditVo para) {
        //String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionKpiRatioService.deleteCommissionKpiRatio(para.getId()));
    }

    /**
     * 查询佣金结算表
     */
    @PostMapping("/selectBillReport")
    public R<PageData<FiBillResVo>> selectBillReport(@RequestBody @Validated FiBIllListReqVo para){
        return R.SuccessPage(fiBillReportService.getBillReport(para));
    }

    /**
     * 导出佣金结算表
     */
    @PostMapping("/exportBillReport")
    public R<CmdResVo> exportBillReport(@RequestBody @Validated FiBIllListReqVo para, HttpServletResponse response) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiBillReportService.saveAsExcel(usrid, para, response));
    }
    /**
     * 批量结账。
    */
    @PostMapping("/billFinished")
    public R<CmdResVo> billFinished(@RequestBody @Validated BatchIdsVo billIds) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiBillReportService.billFinished(usrid,billIds.getReportIds()));
    }

    /**
     * 更改其他费用。
     */
    @PostMapping("/updateOtherFee")
    public R<CmdResVo> updateOtherFee(@RequestBody @Validated OtherFeeVo para) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiBillReportService.updateOtherFee(usrid,para));
    }
    /**
     * 刷新结账表。
     */
    @PostMapping("/refresBillReport")
    public R<CmdResVo> refresBillReport(@RequestBody @Validated RefreshAmtVo para)
    {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiBillReportService.refreshBillReport(usrid, para.getBillMonth()));
    }

    /**
     * 每月在委案件明细LIST
     * @param reqVo
     * @return
     */
    @PostMapping("/repaymentDetailsMonthlyPage")
    public R<PageData<RepaymentDetailsMonthPageResVo>>monthlyPage(@RequestBody @Validated RepaymentDetailsMonthPageReqVo reqVo){
        String key = "";
        Boolean b = false;
        String user=jCommonData.getCurrUsrID();
        key=jCommonData.getReMonthlyKey(user);
        b = jCommonData.setData(key,reqVo, JPubUtils.distExpiration);
        log.info("repaymentDetailsMonthlyPage.setDistQuyKey({},{}):ret={}",key,reqVo,b);

        return R.SuccessPage(repaymentDetailsService.monthlyPage(reqVo));
    }

    /**
     * 每月在委案件明细LIST导出
     * @param reqVo
     * @return
     */
    @PostMapping("/repaymentDetailsMonthlyPageExcel")
    public R<CmdResVo>monthlyPageExcel(@RequestBody @Validated RepaymentDetailsMonthPageReqVo reqVo,HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(repaymentDetailsService.monthlyExcel(user,reqVo,response));
    }

    /**
     * 一键配置 是否计佣金参数
     * @param reqVo
     * @return
     */
    @PostMapping("/repaymentDetailsParameterConfig")
    public R<CmdResVo>parameterConfig(@RequestBody @Validated RepaymentDeParameterCfReqVo reqVo){
        String usrid= jCommonData.getCurrUsrID();
        String key=jCommonData.getReMonthlyKey(usrid);
        Object o=jCommonData.getData(key);
        RepaymentDetailsMonthPageReqVo quyparas=null;
        if(o==null){
            log.warn("parameterConfig:nofind {}",key);
            return R.Error(EnumRCode.NoFindQuyPara);
        }else{
            quyparas=(RepaymentDetailsMonthPageReqVo)o;
        }
        return R.Success(repaymentDetailsService.parameterConfig(usrid,quyparas,reqVo));
    }

    /**
     * 查询每日案件还款统计表
     */
    @PostMapping("/selectCaseStatistics")
    public R<PageData<CaseStatisticsResVo>> selectCaseStatistics(@RequestBody @Validated CaseStatisticsReqVo para){
        return R.SuccessPage(fiCasesRepaymentStatisticsService.getCaseRepayStatistics(para));
    }
    /**
     * 重新计算还款统计数据。
     */
    @PostMapping("/reCalCaseStatistics")
    public R<CmdResVo> reCalCaseStatistics(@RequestBody @Validated CaseStatisticsReCalVo para) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCasesRepaymentStatisticsService.reCalCaseStatistics(usrid,para));
    }

    /**
     * 案件日还款数据导出。
     */
    @PostMapping("/exportCaseStatistics")
    public R<CmdResVo> exportCaseStatistics(@RequestBody @Validated CaseStatisticsReqVo para, HttpServletResponse response) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCasesRepaymentStatisticsService.saveAsExcel(usrid,para,response));
    }

    /**
     * 机构还款统计表查询
     */
    @PostMapping("/deptRepayRpt")
    public R<PageData<DeptReportResVo>> deptRepayRpt(@RequestBody @Validated DeptRepayListReqVo para){
        return R.SuccessPage(fiDeptRepaymentReportService.getDeptRepayReport(para));
    }

    /**
     * 导出机构还款统计表查询
     */
    @PostMapping("/exportDeptRepayRpt")
    public R<CmdResVo> exportDeptRepayRpt(@RequestBody @Validated DeptRepayListReqVo para, HttpServletResponse response) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiDeptRepaymentReportService.saveAsExcel(usrid, para, response));
    }

    /**
     * 刷新机构统计表。
     */
    @PostMapping("/refreshDeptReport")
    public R<CmdResVo> refreshDeptReport(@RequestBody @Validated RefreshAmtVo para)
    {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiDeptRepaymentReportService.refreshReport(usrid, para.getBillMonth()));
    }

    /**
     * 催员还款统计表查询
     */
    @PostMapping("/agentRepayRpt")
    public R<PageData<AgentReportResVo>> deptRepayRpt(@RequestBody @Validated AgentRepayListReqVo para){
        return R.SuccessPage(fiAgentRepayReportService.getAgentRepayReport(para));
    }

    /**
     * 导出催员统计表
     */
    @PostMapping("/exportAgentRepayRpt")
    public R<CmdResVo> exportAgentRepayRpt(@RequestBody @Validated AgentRepayListReqVo para, HttpServletResponse response) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiAgentRepayReportService.saveAsExcel(usrid, para, response));
    }


    /**
     * 刷新机构统计表。
     */
    @PostMapping("/refreshAgentReport")
    public R<CmdResVo> refreshAgentReport(@RequestBody @Validated RefreshAmtVo para)
    {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiAgentRepayReportService.refreshReport(usrid,para.getBillMonth()));
    }

    /**
     * 催收佣金统计表查询
     */
    @PostMapping("/commissionAmtRpt")
    public R<PageData<CommissionAmtResVo>> commissionAmtRpt(@RequestBody @Validated CommissionAmtListReqVo para){
        return R.SuccessPage(fiCommissionAmtService.getCommissionAmtList(para));
    }

    /**
     * 导出催收佣金统计表
     */
    @PostMapping("/exportCommissionAmtRpt")
    public R<CmdResVo> exportCommissionAmtRpt(@RequestBody @Validated CommissionAmtListReqVo para, HttpServletResponse response) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAmtService.saveAsExcel(usrid, para, response));
    }
    /**
     * 刷新佣金统计表。
     */
    @PostMapping("/refreshCommissionRpt")
    public R<CmdResVo> refreshCommissionRpt(@RequestBody @Validated RefreshAmtVo para)
    {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAmtService.refreshReport(usrid,para.getBillMonth()));
    }

    /**
     * 调整佣金统计表。
     */
    @PostMapping("/adjustCommissionAmt")
    public R<CmdResVo> adjustCommissionAmt(@RequestBody @Validated AdjustAmtVo para)
    {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionAmtService.adjustCommissionAmt(usrid, para));
    }
}
